package exp.ranking;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;

import opennlp.tools.tokenize.Tokenizer;
import tools.FileReaderEnhance;
import tools.FileWriterEnhance;
import tools.ObjectTools;
import tools.nlp.TokenizerSimple;
import tools.similarity.RougeN;
import tools.twitter.TweetTidy;
import edu.hit.irlab.nlp.stopwords.StopWords;
import exp.URLContent1;

/**
 * Generate the gold standard for each topic
 * <br> The gold standard is a rank of tweets
 * <br> Use ROUGE-1 to calculate the similarity
 * @author Lanjun
 *
 */
public class Ex4 {
	
	static Vector<String> sw = (new StopWords("data/stopwords.dat")).getStopwords();
	static HashMap<String, Boolean> stopwords = new HashMap<String, Boolean>();
	static {
		for (String stopword : sw) {
			stopwords.put(stopword, true);
		}
	}
	static TokenizerSimple ts = new TokenizerSimple();
	static Tokenizer tokenizer = ts.getTokenizer();
	
	/**
	 * 
	 * @param hcPath Highlights-Contents Mapping, already ranked
	 * @param tPath Path for tweets
	 * @param outputPath output Path
	 * @param TOPNCONTENTS Top-N ranked content sentences for each highlight 
	 * @param TOPNTWEETS Top-N ranked tweets for each content sentences
	 */
	public void run(String hcPath, String tPath, String outputPath, int TOPNCONTENTS, int TOPNTWEETS) {
		
		RougeN r = new RougeN(1, 1);
		
		for (File file : (new File(hcPath).listFiles())) {
			String[] tweets = FileReaderEnhance.readToString(tPath + file.getName(), "UTF-8").split("\n");
			
			@SuppressWarnings("unchecked")
			ArrayList<URLContent1> ucs = (ArrayList<URLContent1>) ObjectTools.readFromFile(file);
			
			StringBuilder sb = new StringBuilder();
			for (URLContent1 uc : ucs) {
				sb.append("\n-----------------------------------------------------------------------------\n");
				sb.append(uc.url);
				sb.append("\n-----------------------------------------------------------------------------\n");
				for (int i = 0; i < uc.sortedContent.size(); i++) {
					sb.append("\nHighlights: ");
					sb.append(uc.highlights.get(i));
					sb.append('\n');
					
					TreeMap<Double, String> map = uc.sortedContent.get(i);
					Set<Double> keys = map.descendingKeySet();
					int count = 0;
					for (Double key : keys) {
						if (key == 0.0) {
							break;
						}
						String content = map.get(key);
						String[] eval = new String[1];
						eval[0] = content;
						TreeMap<Double, String> sorted = new TreeMap<>();
						HashMap<String, Boolean> doneTweets = new HashMap<>();
						for (String tweet : tweets) {
							String tweet1 = TweetTidy.doTidyAll(tweet);
							if (doneTweets.containsKey(tweet1)) {
								continue;
							} else {
								doneTweets.put(tweet1, true);
							}
							r.getRouge().evaluate(tweet1, eval);
							Double f = r.getRouge().getFScore();
							if (sorted.containsKey(f)) {
								sorted.put(f, sorted.get(f) + "\t,,\t" + tweet1);
							} else {
								sorted.put(f, tweet1);
							}
						}
						sb.append("=================================================\nSentence: ");
						sb.append(content);
						sb.append("\n=================================================\n");
						int count1 = 0;
						for (Double f : sorted.descendingKeySet()) {
							if (f == 0.0) {
								break;
							}
							sb.append(f);
							sb.append('\t');
							sb.append(sorted.get(f));
							sb.append('\n');
							count1++;
							if (count1 >= TOPNTWEETS) {
								break;
							}
						}
						count++;
						if (count >= TOPNCONTENTS) {
							break;
						}
					}
				}
			}
			FileWriterEnhance fwe = new FileWriterEnhance(outputPath + file.getName(), "UTF-8");
			fwe.WriteToFile(sb.toString());
		}
	}
	
	public static void main(String[] args) {
		String hcPath = "data/objs/highlights_contents_ROUGE/";
		
		String tPath = "data/filtered/";
		
		String outputPath = "data/ranked_1/";

		int topN = 1;

		int topN1 = 3;
		
		Ex4 ex4 = new Ex4();
		ex4.run(hcPath, tPath, outputPath,  topN, topN1);
	}
}
